package com.yunpuvip.sachet.rest.controller.port;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.yunpuvip.sachet.biz.entity.Player;
import com.yunpuvip.sachet.biz.service.PlayerService;
import com.yunpuvip.sachet.modular.constant.response.ServerResponse;
import com.yunpuvip.sachet.modular.core.exception.GunsException;
import com.yunpuvip.sachet.modular.core.exception.RestServiceExceptionEnum;
import com.yunpuvip.sachet.modular.util.sms.Sendsms;
import com.yunpuvip.sachet.modular.vo.rest.request.UserMsgVo;
import com.yunpuvip.sachet.rest.common.util.VerifyCodeKit;
import com.yunpuvip.sachet.rest.common.vo.LoginVo;
import com.yunpuvip.sachet.rest.common.vo.PlayerVo;
import com.yunpuvip.sachet.rest.config.properties.RestProperties;
import com.yunpuvip.sachet.rest.controller.common.RestBaseController;
import com.yunpuvip.sachet.rest.modular.auth.util.JwtTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.OBJECT_NOT_EXIST;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.HashMap;

/**
 * @ auther wushaohong
 * @ date  2019/8/10
 **/
@RequestMapping("userInfo")
@RestController
@Slf4j
public class UserController extends RestBaseController {

    @Autowired
    private PlayerService playerService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    RestProperties restProperties;

    @RequestMapping("loginOut")
    public ResponseData loginOut(){
        Player player = this.getPlayer();
        boolean b = playerService.userLoginOut(player);
        if(b){
            return ResponseData.success();
        }
        return ResponseData.error("退出失败，请重试");
    }


    @RequestMapping("fillUserMsg")
    public ServerResponse  fillUserMsg(   UserMsgVo userMsgVo){
        if(ObjectUtil.isNull(userMsgVo)){
            return ServerResponse.creatByErrorEnum(RestServiceExceptionEnum.PARAMETER_VALUE_EMPTY);
        }
        Player player = getPlayer();
        userMsgVo.setId(player.getId());
        boolean b = playerService.updateUserMsg(userMsgVo);
        return ServerResponse.creatBySuccess(userMsgVo);
    }


    /**
     * 上传图片
     */

    @RequestMapping(method = RequestMethod.POST,  path = "uploadImg")
    public ServerResponse upload(@RequestPart("file") MultipartFile picture) {
        String fileUploadPath = restProperties.getRestUploadPath();
        String image = playerService.uploadSingleImage(fileUploadPath, picture);
        //返回前端一个图片的URL，带上img，做nginx的路由配置
        return ServerResponse.creatBySuccess(image);
    }

    /**
     *
     * 短信验证
     * 绑定手机号码
     * @param loginVo
     *
     * @return
     */
    @RequestMapping(value = "/verifyChange")
    public ServerResponse playerLogin(@Valid LoginVo loginVo ){
        String phoneNumber = loginVo.getPhoneNumber();
        //检查验证码
        boolean res = false;
        res=VerifyCodeKit.checkVerifyCode(loginVo.getPhoneNumber(), loginVo.getVerifyCode());
        if(!res){
            return ServerResponse.creatByErrorEnum(RestServiceExceptionEnum.PHONE_CODE_NOT_MISMATCH);
        }
        Player player = getPlayer();
        System.err.println("验证码无误");
        HashMap<String, Object> map = new HashMap<>();
        String token = jwtTokenUtil.getToken(phoneNumber);
        Date tokenExpireTime = jwtTokenUtil.getTokenExpireTime();

        /**
         * 更新用户的token
         */
        playerService.changePhoneNum(player.getId(),phoneNumber,token,tokenExpireTime);
        map.put("newPhone",phoneNumber);
        map.put("token",token);
        return  ServerResponse.creatBySuccess(map);
    }

    /**
     *
     * 获取用户最新消息
     */
    @RequestMapping(value = "/recent")
    public ServerResponse playerInfo(){
         log.info("1获取用户信息");
        Player player = getPlayer();
        log.info("2获取用户信息：{}",player);
        PlayerVo playerVo = new PlayerVo();
        playerVo.setBalance(player.getBalance());
        log.info("3返回用户信息：{}",player);

        return  ServerResponse.creatBySuccess(player);
    }

    /**
     * 无需再写
     */
    /*@RequestMapping(value = "/changePhoneCode")
    public ServerResponse getVerifyCode(@RequestParam String oldPhoneNumber,@RequestParam String newPhoneNumber, @RequestParam(required = false,defaultValue = "1")  Integer sendType){
        *//**
         * 先判断该号码是否已经被绑定
         *//*
        Player playerByPhone = playerService.getPlayerByPhone(oldPhoneNumber);
        if(ObjectUtil.isNotNull(playerByPhone)){
            throw new GunsException(RestServiceExceptionEnum.PHONE_REPEAT_BINDING);
        }
        //获取短信验证码
        String code = Sendsms.sendSmsCodeCommon(newPhoneNumber, sendType);
        HashMap<String,String> map = new HashMap<>();
        //缓存验证码
        VerifyCodeKit.putVerifyCode(newPhoneNumber,code);
        return ServerResponse.creatBySuccessMessage("短信验证码发送成功");
    }*/
    public static void main(String[] args) {
        float a=12343.99f;
        System.out.println(a);
    }
}
